<?php

namespace App\Http\Controllers;
use App\Mail\RegMail;
use Illuminate\Http\Request;
use App\User;
use App\Http\Requests\UserRequest;
use Mail;
use Auth;
class RegisterController extends Controller
{
    public function register(){
    	return view('user.register');
	}
	public function store(UserRequest $request)
	{
		$user=User::create([
			'name'=>$request->name,
			'email'=>$request->email,
			'password'=>bcrypt($request->password),
			'email_token'=>str_random (10)
		]);
		Mail::to ($user)->send(new RegMail($user->email_token));
		session()->flash ('success','注册成功！请去你的邮箱激活账户！');
		return redirect ()->back();
	}
	public function active($token){
    	//将token值和数据库中的email_token进行比对
		$user=User::where('email_token',$token)->first();
		if($user){
			$user->status=1;
			$user->save ();
			Auth::login ($user);
			session()->flash('success','账号激活成功！');
			return redirect ()->route('user.index');
		}else{
			session()->flash('danger','账号不存在！');
			return redirect ()->route('zhuce');
		}
	}
}
